package com.gitee.wsl.struct.dict

class CacheDictionaryMapImp<T:Any,V:Any>(val valueMap: MutableMap<T,V> = mutableMapOf(), val creator:(T)->V):
    DictionaryMap<T, V> {
    override val name:String = "cache"
    override fun put(key:T, value:V){
        valueMap[key]= value
    }

    override fun get(key:T):V{
        var ret=valueMap[key]
        if(ret==null){
            ret = creator(key)
            put(key,ret)
        }
        return ret
    }

    override fun delete(key:T){
        valueMap.remove(key)
    }

    override fun map(): Map<T, V> =valueMap
}